/**
 * 录音和变声合集预制
 * */
import btButton from "./btButton";
import O1HD from "../../engines/gameEngine";
import ttApi from "../../engines/services/miniGameContent/tt/ttApi";

export enum RECORD_VOICE_STATE {
    NULL,
    READY,
    RECORD,
    OVER,
    PLAY,
}

export interface EffectType {
    //
    name: string;
    //数值
    numScale: number;
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class recordVoiceChange extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    btList: cc.Node = null;
    @property(cc.Node)
    effectBtNode: cc.Node = null;
    @property(cc.Label)
    lbState: cc.Label = null;
    @property(cc.EditBox)//输入框
    editInput: cc.EditBox = null;

    onLoad() {
        //
        this.OnLoadSetBts();
    }

    private m_currState: RECORD_VOICE_STATE = RECORD_VOICE_STATE.READY;
    private m_recordTime: number = 0;
    private readonly m_effectList: EffectType[] = [
        {name: '调皮猴', numScale: 0.55},
        {name: '机灵兔', numScale: 0.75},
        {name: '复读机', numScale: 1},
        {name: '憨憨象', numScale: 1.25},
        {name: '老怪物', numScale: 1.5},
    ];

    start() {
        this.m_currState = RECORD_VOICE_STATE.READY;
    }

    private m_timeBind: () => void = null;

    getTimeFunction() {
        if (this.m_timeBind == null) {
            this.m_timeBind = this.OnTimer.bind(this);
        }
        return this.m_timeBind;
    }

    OnTimer() {
        this.m_recordTime++;
    }

    protected OnBtnStartRecordVoice() {
        if (this.m_currState != RECORD_VOICE_STATE.NULL &&
            this.m_currState != RECORD_VOICE_STATE.READY &&
            this.m_currState != RECORD_VOICE_STATE.OVER) {
            O1HD.getModules().uiControlModule.showMessageBox('还不能开始录音');
            return;
        }
        O1HD.getServices().miniGameSrv.getAppApiInstance().StartRecordSound();
        this.m_currState = RECORD_VOICE_STATE.RECORD;
        this.m_recordTime = 0;
        this.schedule(this.getTimeFunction(), 1);
    }

    protected OnBtnStopRecordVoice() {
        if (this.m_currState != RECORD_VOICE_STATE.RECORD) {
            O1HD.getModules().uiControlModule.showMessageBox('还没有开始录音');
            return;
        }
        this.unschedule(this.getTimeFunction());
        O1HD.getServices().miniGameSrv.getAppApiInstance().StopRecordSound(() => {
            this.m_currState = RECORD_VOICE_STATE.OVER;
        });
    }

    protected OnBtnExit() {
        this.node.destroy();
    }

    protected OnBtnPlaySetNum() {
        this.OnBtnListEffect({name: '手动调', numScale: Number(this.editInput.string)});
    }

    private OnBtnListEffect(data: EffectType) {
        if (this.m_currState != RECORD_VOICE_STATE.OVER) {
            if (this.m_currState == RECORD_VOICE_STATE.PLAY) {
                O1HD.getModules().uiControlModule.showMessageBox('正在播放中');
            } else {
                O1HD.getModules().uiControlModule.showMessageBox('没有录音操作');
            }
            return;
        }
        this.m_currState = RECORD_VOICE_STATE.PLAY;
        O1HD.getServices().miniGameSrv.getAppApiInstance().PlayLastRecordSound(data.numScale, () => {
            this.m_currState = RECORD_VOICE_STATE.OVER;
        }, () => {
            this.m_currState = RECORD_VOICE_STATE.OVER;
        })
    }

    private OnLoadSetBts() {
        for (let i = 0; i < this.m_effectList.length; i++) {
            const data = this.m_effectList[i];
            let currNode = this.effectBtNode;
            if (i > 0) {
                currNode = cc.instantiate(currNode);
                this.btList.addChild(currNode);
            }
            let currBt = currNode.getComponent(btButton);
            currBt.setButtonText(data.name);
            currNode.off('click');
            currNode.on('click', () => {
                this.OnBtnListEffect(data);
            })
        }
    }

    private OnUpdateState() {
        switch (this.m_currState) {
            case RECORD_VOICE_STATE.NULL: {
                this.lbState.string = '等待录音';
            }
            case RECORD_VOICE_STATE.READY: {
                this.lbState.string = '播放完毕';
                break;
            }
            case RECORD_VOICE_STATE.RECORD: {
                this.lbState.string = `录音中:[${this.m_recordTime}s]`;
                break;
            }
            case RECORD_VOICE_STATE.OVER: {
                this.lbState.string = `录音完毕`;
                break;
            }
            case RECORD_VOICE_STATE.PLAY: {
                this.lbState.string = `正在播放`;
                break;
            }
        }
    }

    update(dt) {
        this.OnUpdateState();
    }
}






























